import { isExistUser, isExistPhone, isExistEmail, registered } from "../js/interface.js"

$("#user").blur(judgeUser);
async function judgeUser(){
    var user = $("#user").val().trim();
    $("#user").val(user);

    var reg = /^[a-zA-Z_]\w{5,11}$/;

    if (reg.test(user)) {

        var result = await isExistUser({ user: user });
        var { status, msg } = result;
        if (status) {
            $("#user-mes").html("√用户名可用");
            $("#user-mes").addClass("right").removeClass("err");
            return true;
        } else {
            $("#user-mes").html("用户名已存在");
            $("#user-mes").addClass("err");
        }
    } else {
        $("#user-mes").html("用户名由数字、字母、下划线组成,6-12位,且不能以数字开头");
        $("#user-mes").addClass("err");
    }
    return false;
}

$("#pwd").blur(judgePwd);
async function judgePwd(){
    var pwd = $("#pwd").val().trim();
    $("#pwd").val(pwd);

    var reg = /^\w{6,12}$/;

    if (reg.test(pwd)) {
        $("#pwd-mes").html("√");
        $("#pwd-mes").addClass("right").removeClass("err");
        return true;
    } else {
        $("#pwd-mes").html("密码由数字、字母、下划线组成,6-12位");
        $("#pwd-mes").addClass("err");
    }
    return false;
}

$("#repwd").blur(judgeRepwd);
async function judgeRepwd(){
    var pwd = $("#pwd").val().trim();
    var repwd = $("#repwd").val().trim();
    if(pwd){
        if(pwd == repwd){
            $("#repwd-mes").html("√");
            $("#repwd-mes").addClass("right").removeClass("err");
            return true;
        }else{
            $("#repwd-mes").html("两次密码不一致");
            $("#repwd-mes").addClass("err");
        }
    }else{
        $("#repwd-mes").html("请先设置密码");
        $("#repwd-mes").addClass("err");
    }
}

$("#phone").blur(judgePhone);
async function judgePhone(){
    var phone = $("#phone").val().trim();
    $("#phone").val(phone);

    var reg = /^1[3-9]\d{9}$/;

    if (reg.test(phone)) {

        var result = await isExistPhone({ phone });
        var { status, msg } = result;
        if (status) {
            $("#phone-mes").html("手机号可用");
            $("#phone-mes").addClass("right").removeClass("err");
            // isPhoneOk = true;
            return true;
        } else {
            $("#phone-mes").html("此手机号已被注册");
            $("#phone-mes").addClass("err");
        }

    } else {
        $("#phone-mes").html("请输入正确的手机号");
        $("#phone-mes").addClass("err");
    }

    return false;
}

$("#email").blur(judgeEmail);
async function judgeEmail(){
    var email = $("#email").val().trim();
    $("#email").val(email);

    var reg = /^\w+@\w+\.(com|cn|edu)$/;

    if (reg.test(email)) {
        var result = await isExistEmail({ email });
        var { status, msg } = result;
        if (status) {
            $("#email-mes").html("邮箱可用");
            $("#email-mes").addClass("right").removeClass("err");
            return true;
        } else {
            $("#email-mes").html("此邮箱已被注册");
            $("#email-mes").addClass("err");
        }

    } else {
        $("#email-mes").html("请输入正确的邮箱");
        $("#email-mes").addClass("err");
    }

    return false;
}

$("#btn").click(async function(){
    var list = await Promise.all([judgeUser(),judgePwd(),judgeRepwd(),judgePhone(),judgeEmail()]);
    var flag = list.every(item => item === true);
    if (flag) {
        var user = $("#user").val().trim();
        var pwd = $("#pwd").val().trim();
        var phone = $("#phone").val().trim();
        var email = $("#email").val().trim();
        console.log({ user, pwd, phone, email });
        var result = await registered({ user, pwd, phone, email });
        console.log(result);
        var { status, msg } = result;
        if (status) {
            location.href = "login.html";
        } else {
            alert(msg);
        }
    }
})